package com.dkd.manage.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.stream.Collectors;

import com.dkd.common.utils.DateUtils;
import com.dkd.manage.domain.Building;
import com.dkd.manage.domain.Landlord;
import com.dkd.manage.domain.bo.LandlordBuildingBo;
import com.dkd.manage.domain.vo.LandlordVO;
import com.dkd.manage.mapper.LandlordMapper;
import com.dkd.manage.service.ILandlordService;
import com.dkd.manage.domain.LandlordBuildingRel;
import com.dkd.manage.domain.dto.LandlordDTO;
import com.dkd.manage.mapper.LandlordBuildingRelMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 房东信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-07-11
 */
@Service
public class LandlordServiceImpl implements ILandlordService {
    @Autowired
    private LandlordMapper landlordMapper;
    @Autowired
    private LandlordBuildingRelMapper landlordBuildingRelMapper;

    /**
     * 查询房东信息
     *
     * @param id 房东信息主键
     * @return 房东信息
     */
    @Override
    public Landlord selectLandlordById(Long id) {
        return landlordMapper.selectLandlordById(id);
    }

    /**
     * 查询房东信息列表
     *
     * @param landlord 房东信息
     * @return 房东信息
     */
    @Override
    public List<LandlordVO> selectLandlordList(Landlord landlord) {
        List<LandlordVO> landLordVOS = new ArrayList<>();
        List<Landlord> landlords = landlordMapper.selectLandlordList(landlord);
        if (landlords != null && !landlords.isEmpty()) {
            // 查询关联表
            List<LandlordBuildingBo> landlordBuildingBos = landlordBuildingRelMapper.selectBuildingsByLids(landlords.stream().map(Landlord::getId).collect(Collectors.toList()));
            landlords.forEach(new Consumer<Landlord>() {
                @Override
                public void accept(Landlord landlord) {
                    LandlordVO landlordVO = new LandlordVO();
                    BeanUtils.copyProperties(landlord, landlordVO);
                    List<Building> buildingList = new ArrayList<>();
                    for (LandlordBuildingBo landlordBuildingBo : landlordBuildingBos) {
                        if (Objects.equals(landlordBuildingBo.getLandlordId(), landlord.getId())) {
                            Building building = new Building();
                            BeanUtils.copyProperties(landlordBuildingBo, building);
                            buildingList.add(building);
                        }
                    }
                    landlordVO.setBuildingCount(buildingList.size());
                    landlordVO.setBuildingList(buildingList);
                    landLordVOS.add(landlordVO);
                }
            });
        }

        return landLordVOS;
    }

    /**
     * 新增房东信息
     *
     * @param landlordDTO 房东信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertLandlord(LandlordDTO landlordDTO) {
        landlordDTO.setCreateTime(DateUtils.getNowDate());
        int i = landlordMapper.insertLandlord(landlordDTO);

        Long landlordId = landlordDTO.getId();

        List<Long> buildingIds = landlordDTO.getBuildingIds();
        if (buildingIds != null && !buildingIds.isEmpty()) {
            List<LandlordBuildingRel> landlordBuildingRelList = new ArrayList<>();
            for (Long buildingId : buildingIds) {
                landlordBuildingRelList.add(LandlordBuildingRel.builder().buildingId(buildingId).landlordId(landlordId).createTime(DateUtils.getNowDate()).build());
            }
            landlordBuildingRelMapper.insertBatch(landlordBuildingRelList);
        }

        return i;
    }

    /**
     * 修改房东信息
     *
     * @param landlord 房东信息
     * @return 结果
     */
    @Override
    public int updateLandlord(Landlord landlord) {
        landlord.setUpdateTime(DateUtils.getNowDate());
        return landlordMapper.updateLandlord(landlord);
    }

    /**
     * 批量删除房东信息
     *
     * @param ids 需要删除的房东信息主键
     * @return 结果
     */
    @Override
    public int deleteLandlordByIds(Long[] ids) {
        return landlordMapper.deleteLandlordByIds(ids);
    }

    /**
     * 删除房东信息信息
     *
     * @param id 房东信息主键
     * @return 结果
     */
    @Override
    public int deleteLandlordById(Long id) {
        return landlordMapper.deleteLandlordById(id);
    }
}
